package com.yun.algorithmproblem.leetcodenotcomplete;

import java.util.Arrays;


public class Leetcode2617 {
    public int minimumVisitedCells(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        int[][] dist = new int[m][n];
        for (int[] line : dist) {
            Arrays.fill(line, Integer.MAX_VALUE);
        }
        dist[0][0] = 1;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int v = grid[i][j];
                if(dist[i][j]==Integer.MAX_VALUE){
                    continue;
                }
                for (int k = 1; k <= v; k++) {
                    if (j + k < n) {//往右走
                        dist[i][j + k] = Math.min(dist[i][j + k], dist[i][j] + 1);
                    }
                    if (i + k < m) {//往下走
                        dist[i + k][j] = Math.min(dist[i + k][j], dist[i][j] + 1);
                    }
                }
            }
        }
        return dist[m - 1][n - 1] == Integer.MAX_VALUE ? -1 : dist[m - 1][n - 1];
    }

    public static void main(String[] args) {
        int[][] grid={{0,1,0}};
        Leetcode2617 obj=new Leetcode2617();
        obj.minimumVisitedCells(grid);
    }
}
